package Java.BlackHorse.a06ThreadTest.Test3;

import java.util.ArrayList;
import java.util.Collections;

public class MyThread extends Thread{
    ArrayList<Integer> list = new ArrayList<>();

    public MyThread(String name, ArrayList<Integer> list) {
        super(name);
        this.list = list;
    }

//    static ArrayList<Integer> list1 = new ArrayList<>();
//    static ArrayList<Integer> list2 = new ArrayList<>();

    @Override
    public void run() {
        ArrayList<Integer> boxlist = new ArrayList<>();
        while(true){
            synchronized (MyThread.class) {
                if(list.size() == 0) {
                    if("抽奖箱一".equals(getName())){
                        System.out.println("抽奖箱一"+boxlist);
                        System.out.println("最大奖为："+getMax(boxlist));
                        System.out.println("总金额为："+getSum(boxlist));
                    }else{
                        System.out.println("抽奖箱二"+boxlist);
                        System.out.println("最大奖为："+getMax(boxlist));
                        System.out.println("总金额为："+getSum(boxlist));
                    }
                    break;
                }
                Collections.shuffle(list);

                if("抽奖箱一".equals(getName())){
                    boxlist.add(list.remove(0));
                }else{
                    boxlist.add(list.remove(0));
                }
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public int getSum(ArrayList<Integer> list){
       int sum=0;
       for(Integer i : list){
           sum+=i;
       }
       return sum;
    }

    public int getMax(ArrayList<Integer> list){
        int max=list.get(0);
        for(Integer i : list){
            if(i > max)max = i;
        }
        return max;
    }

}
